package win.zxshuo.www.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Service;
import win.zxshuo.www.model.AuthorModel;
import win.zxshuo.www.repository.AuthorRepository;

import java.util.Date;

@Service
@Configurable
public class AuthorService {
    @Autowired
    AuthorRepository authorRepository;

    public Long getAuthorId(String name){
        AuthorModel authorModel = authorRepository.getByName(name);
        if (authorModel!= null){
            return authorModel.getId();
        }else {
            AuthorModel authorModel1 = new AuthorModel();
            authorModel1.setCreateDate(new Date());
            authorModel1.setModifyDate(new Date());
            authorModel1.setName(name);
            authorRepository.save(authorModel1);
            return authorModel1.getId();
        }
    }

    public AuthorModel getAuthorBy(String name){
        AuthorModel authorModel = authorRepository.getByName(name);
        if (authorModel != null){
            return authorModel;
        }else {
            AuthorModel authorModel1 = new AuthorModel();
            authorModel1.setCreateDate(new Date());
            authorModel1.setModifyDate(new Date());
            authorModel1.setName(name);
            authorRepository.save(authorModel1);
            return authorModel1;
        }
    }
}
